<?php

namespace app\backend\controller;

use app\backend\ApiBaseController;
use think\facade\Filesystem;

class Upload extends ApiBaseController
{

    public function index()
    {
        $file = $this->request->file('file');
        if(empty($file)) {
            return $this->jsonError('请选择上传文件');
        }

        $type = $this->request->param('type', 0);

        switch ($type) {
            case 1: $fileDir = 'category';break;
            case 2: $fileDir = 'product';break;
            default: $fileDir = 'general';break;
        }

        $savename = Filesystem::disk('public')->putFile($fileDir, $file, 'md5');
        $savename = strtr($savename, '\\', '/');

        return $this->jsonSuccess('upload succeed', ['url' => $savename]);
    }
}